//
//  HWWebZhibViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/11/20.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWWebZhibViewController.h"
#import <WebKit/WebKit.h>
@interface HWWebZhibViewController ()<WKUIDelegate,WKNavigationDelegate>
@property (strong, nonatomic)  WKWebView *webView;



@end

@implementation HWWebZhibViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    WKWebView *webView = [[WKWebView  alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    self.webView = webView;
    self.webView.UIDelegate = self;
    self.webView.navigationDelegate = self;
    
    
    [self.view addSubview:webView];
    
     NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:self.url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    
 
    [request setHTTPMethod:@"GET"];
 //   [request setValue:@"del.94php.com://" forHTTPHeaderField: @"Referer"];
    
    
    
    [webView loadRequest:request];
    
}







#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WKNavigationAction *)navigationAction decisionHandler:(nonnull void (^)(WKNavigationActionPolicy))decisionHandler {
    
    if (!navigationAction.request.allHTTPHeaderFields[@"Referer"]) {
          NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:navigationAction.request.URL];
          [request setValue:@"wechat.baiwanmeng.com://" forHTTPHeaderField:@"Referer"];
          [webView loadRequest:request];
      }

    
    

    WKNavigationActionPolicy actionPolicy = WKNavigationActionPolicyAllow;
    // 拦截的url字符串
    NSString *urlString = navigationAction.request.URL.absoluteString;
    urlString = [urlString stringByRemovingPercentEncoding];

    // 判断是否为支付链接，可能会加载多个链接，只有包含"weixin://wap/pay?"才是可以跳转微信APP的链接，注意应用间跳转关键的scheme："weixin://"
    if([urlString containsString:@"weixin://wap/pay?"]) {
        actionPolicy = WKNavigationActionPolicyCancel; // 不允许webView加载

        // 判断是否安装微信
        if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]]) {
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"未检测到微信客户端，请安装后重试" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
               // [self.navigationController popViewControllerAnimated:YES];
            }];
            [alertVC addAction:sureAction];
            [self presentViewController:alertVC animated:YES completion:nil];
            
            decisionHandler(actionPolicy);
            return;
        }

        // 跳转微信进行支付
        if (@available(iOS 10.0, *)) { // 10.0以上的版本
            if([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:options:completionHandler:)]) {
                [[UIApplication sharedApplication] openURL:navigationAction.request.URL options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @NO} completionHandler:nil];
            }
        } else { // 10.0以下的版本
            if ([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:)]) {
                [[UIApplication sharedApplication] openURL:navigationAction.request.URL];
            }
        }
    }
    // 必须加的一行代码，不然会Crash
    decisionHandler(actionPolicy);
    
    
    
}






@end
